<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\db\Query;

/**
 * 消息中心
 *
 * @icon fa fa-circle-o
 */
class Chatroom extends Backend
{

    /**
     * Chatroom模型对象
     * @var \app\admin\model\Chatroom
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Chatroom;
        $this->view->assign("informationIdList", $this->model->getInformationIdList());
        $this->view->assign("isMarginList", $this->model->getIsMarginList());
        $this->view->assign("groupTypeList", $this->model->getGroupTypeList());
        $this->view->assign("typeList", $this->model->getTypeList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            // var_dump($where);
            // unset($where->titlestr);
            // 解码 URL 编码的 JSON 字符串
            $params = $this->request->param();

            // 输出过滤后的参数数组
            // print_r($params);
            $jsonString = urldecode($params['filter']);

            // 解析 JSON 字符串为数组
            $filterArray = json_decode($jsonString, true);
            //筛选
            // 输出解析后的数组
            
            // print_r($filterArray);
            $where='1=1';
            $wherec=[];
            if(isset($filterArray['titlestr']))
            {
                $where1['user.username|user.tel|userto.username|userto.tel']=['like','%'.$filterArray['titlestr'].'%'];
                $where1['group_type']='1';
                $where2['chatgroup.name']=['like','%'.$filterArray['titlestr'].'%'];
                $where2['group_type']='2';           
                // $wherec=[$where1,$where2];


                $where.=' and (((user.username like "%'.$filterArray['titlestr'].'%" or user.tel like "%'.$filterArray['titlestr'].'%" or userto.username like "%'.$filterArray['titlestr'].'%" or userto.tel like "%'.$filterArray['titlestr'].'%") and group_type = "1") or (chatgroup.name  like "%'.$filterArray['titlestr'].'%" and group_type = "2"))';
            }
            
            
        //    var_dump($filterArray['information_id']);die;
            // if(isset($filterArray['information_id']))
            // {
            //     if($filterArray['information_id']=='1')
            //     {
            //         // $where['information_id']=['not in','0,p'];
            //         $where.=' and information_id not in ("0","p")';
            //     }
            //     else
            //     {
            //         // $where['information_id']=$filterArray['information_id'];
            //         $where.=' and information_id = "'.$filterArray['information_id'].'"';
            //     }
                
            // }
            if(isset($filterArray['group_type']))
            {
                // $where['group_type']=$filterArray['group_type'];
                $where.=' and group_type='.$filterArray['group_type'];
            }
            if(isset($filterArray['type']))
            {
                // $where['chatroom.type']=$filterArray['type'];
                $where.=' and chatroom.type='.$filterArray['type'];
            }
            if(isset($filterArray['information_id']))
            {
                // $where['chatroom.type']=$filterArray['type'];
               
                $where.=' and information.name like "%'.$filterArray['information_id'].'%"';
            }

            $list = $this->model
                    ->with(['user','userto','chatgroup','information'])
                    ->where($where)
                    ->order($sort, $order)
                    ->group('group_room')
                    ->paginate($limit);
            // echo $this->model->getlastsql();die;

            foreach ($list as $row) {
                
                $row->getRelation('user')->visible(['username','tel']);
                
				$row->getRelation('userto')->visible(['username','tel']);
                $row->getRelation('chatgroup')->visible(['name']);
                // $row->getRelation('userto')->visible(['username','tel']);
                //信息，平台，单聊/群聊
                if($row['information_id']=='0')
                {
                    //单聊
                    // $information_str='单聊/群聊';
                    $information_str='无';
                }
                else if($row['information_id']=='P')
                {
                    //平台
                    // $information_str='平台客服';
                    $information_str='无';
                }
                else
                {
                    //信息
                    // $information_str='信息';
                    $title=db('information')->where('id',$row['information_id'])->value('name');
                    $information_str=$title;
                }
                $row->data('information_id',$information_str);

                //联系人列表
                //如果是单聊，群聊
                // var_dump($row->userto->tel);
                if($row['group_type']=='1' || $row['group_type']=='3' || $row['group_type']=='4')
                {
                    $row->data('titlestr','用户名'.$row->userto->username.'，手机号'.$row->userto->tel.'---用户名'.$row->user->username.'，手机号'.$row->user->tel);
                }
                if($row['group_type']=='2')
                {
                    $row->data('titlestr','群名称'.$row->chatgroup->name);
                }
            }
            // die;

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

}
